package com.huey.db.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.huey.common.context.BaseContextHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author huey China.
 * @Description : 自动填充
 * @Date Created in 2021/6/21 下午6:04
 */
@Component
public class DbMetaObjectHandler implements MetaObjectHandler {

    private static final String CREATE_BY_FIELD = "createBy";
    private static final String CREATE_DATE_BY_FIELD = "createDate";
    private static final String UPDATE_BY_BY_FIELD = "updateBy";
    private static final String UPDATE_DATE_BY_FIELD = "updateDate";

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, CREATE_BY_FIELD, String.class, BaseContextHandler.getUserID());
        this.strictInsertFill(metaObject, CREATE_DATE_BY_FIELD, Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, UPDATE_BY_BY_FIELD, String.class, BaseContextHandler.getUserID());
        this.strictUpdateFill(metaObject, UPDATE_DATE_BY_FIELD, Date.class, new Date());
    }
}
